#include "color.h"
#include <cstdlib>
#include <cmath>
#include <iostream>

Color::Color(unsigned char _red, unsigned char _green, unsigned char _blue, unsigned char _alpha) : red(_red), green(_green), blue(_blue), alpha(_alpha){
}

Color::Color(double _red, double _green, double _blue){
    if(_red < 0) red = 0;
    else if(_red > 255) red = 255;
    else red = ((unsigned char)floor(_red));

    if(_green < 0) green = 0;
    else if(_green > 255) green = 255;
    else green = ((unsigned char)floor(_green));

    if(_blue < 0) blue = 0;
    else if(_blue > 255) blue = 255;
    else blue = ((unsigned char)floor(_blue));

    alpha = 255;
}

Color::Color(void) : red(0) , green(0) , blue(0), alpha(255){
}

Color::Color(const Color& _color) : red(_color.red), green(_color.green), blue(_color.blue), alpha(_color.alpha){
}

Color::~Color(void){
}

void Color::print() const{
    std::cout << "Vermelho: " << (int)red << "\tVerde: " << (int)green << "\tAzul: " << (int)blue << "\tAlfa: " << (int)alpha << std::endl;
}

bool Color::inGrayscale() const{
    if(red == green && red == blue) return true;

    return false;
}

Color& Color::operator=(const Color& _color) {
	if(this != &_color) {
		if((double)_color.red < 0) red = 0;
		else if((double)_color.red > 255) red = 255;
		else red = _color.red;

		if((double)_color.green < 0) green = 0;
		else if((double)_color.green > 255) green = 255;
		else green = _color.green;

		if((double)_color.blue < 0) blue = 0;
		else if((double)_color.blue > 255) blue = 255;
		else blue = _color.blue;

		if((double)_color.alpha < 0) alpha = 0;
		else if((double)_color.alpha > 255) alpha = 255;
		else alpha = _color.alpha;
	}
	return *this;
}

unsigned char Color::operator[](int& _index) const {
	switch(_index) {
		case 1: return red;
		case 2: return green;
		case 3: return blue;
		case 4: return alpha;
		default: exit(1);
	}
}

Color Color::operator+(const Color& _color) const {
    double r = (double)_color.red + (double)red;
    double g = (double)_color.green + (double)green;
    double b = (double)_color.blue + (double)blue;

	return Color(r, g, b);
}

Color Color::operator*(const Color& _color) const {
    double r = (double)_color.red * (double)red;
    double g = (double)_color.green * (double)green;
    double b = (double)_color.blue * (double)blue;

	return Color(r, g, b);
}

Color Color::operator-(const Color& _color) const {
    double r = (double)_color.red - (double)red;
    double g = (double)_color.green - (double)green;
    double b = (double)_color.blue - (double)blue;

	return Color(r, g, b);
}

Color Color::operator/(const Color& _color) const {
    double r = (double)_color.red / (double)red;
    double g = (double)_color.green / (double)green;
    double b = (double)_color.blue / (double)blue;

	return Color(r, g, b);
}

Color operator*(const Color& _color, const double _value) {
    double r = (double)_color.red * _value;
    double g = (double)_color.green * _value;
    double b = (double)_color.blue * _value;

	return Color(r, g, b);
}

Color operator*(const double _value, const Color& _color) {
	return _color*_value;
}

Color operator-(const double _value, const Color& _color) {
    double r = _value - (double)_color.red;
    double g = _value - (double)_color.green;
    double b =  _value - (double)_color.blue;

	return Color(r, g, b);
}
